package com.gui.mianshiya.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gui.mianshiya.mapper.ExerciseRecordsMapper;
import com.gui.mianshiya.mapper.QuestionMapper;
import com.gui.mianshiya.mapper.WishCollectionMapper;
import com.gui.mianshiya.model.dto.question.QuestionSignRequest;
import com.gui.mianshiya.model.entity.ExerciseRecords;
import com.gui.mianshiya.model.entity.Question;
import com.gui.mianshiya.model.entity.WishCollection;
import com.gui.mianshiya.model.vo.QuestionVO;
import com.gui.mianshiya.model.vo.SignQuestionVo;
import com.gui.mianshiya.service.ExerciseRecordsService;
import com.gui.mianshiya.service.QuestionService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Title: ExerciseRecordsService
 * @Author quit
 * @Package com.gui.mianshiya.service
 * @Date 2025/4/23 15:09
 * @description:
 */
@Service
public class ExerciseRecordsServiceImpl extends ServiceImpl<ExerciseRecordsMapper, ExerciseRecords> implements ExerciseRecordsService {

    @Resource
    private ExerciseRecordsMapper exerciseRecordsMapper;

//    @Resource
//    private QuestionService questionService;
      @Resource
      private QuestionMapper  questionMapper;

    @Override
    public Page<SignQuestionVo> getQuestionList(QuestionSignRequest questionSignRequest) {
        LambdaQueryWrapper<ExerciseRecords> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExerciseRecords::getUserId,questionSignRequest.getUserId());
        List<ExerciseRecords> list = this.list(queryWrapper);
        if (list.isEmpty()) {
            return new Page<>(questionSignRequest.getCurrent(), questionSignRequest.getPageSize(), 0);
        }
        List<Long> questionIds = list.stream()
                .map(ExerciseRecords::getQuestionId)
                .collect(Collectors.toList());
        // 3. 构建题目分页查询
        List<Question> questions = questionMapper.selectBatchIds(questionIds);
        Page<Question> page = new Page<>(questionSignRequest.getCurrent(), questionSignRequest.getPageSize());
        LambdaQueryWrapper<Question> questionQuery = new LambdaQueryWrapper<>();
        questionQuery
                .in(Question::getId, questionIds)
                .orderByDesc(Question::getCreateTime);
        Page<Question> questionPage = questionMapper.selectPage(page, questionQuery);
        List<SignQuestionVo> signQuestionVos = new ArrayList<>();
        List<QuestionVO> questionVO = new ArrayList<QuestionVO>();
        // 4. 执行分页查询
        questionPage.convert(question -> {
            questionVO.add(QuestionVO.objToVo(question));
            return null;
        });
        questionVO.forEach(item -> {
        list.forEach(question -> {
           if (question.getQuestionId() .equals( item.getId())) {
               SignQuestionVo signQuestionVo = new SignQuestionVo();
               System.out.println(question.getQuestionId()+"====" + item.getId());
               BeanUtils.copyProperties(item, signQuestionVo);
               signQuestionVo.setTotalCount(question.getTotalCount());
               signQuestionVos.add(signQuestionVo);
           }
            });
        });
        return new Page<SignQuestionVo>().setRecords(signQuestionVos).setTotal(questionPage.getTotal());
    }
}
